﻿using System;
using System.Linq;
using System.Web.UI.WebControls;

namespace ECommerce.Controls
{
	public partial class OrderList : BaseUserControl
	{
		private Service.IOrderService _OrderService;
		private Service.IProductService _ProductService;
		private string _OrderType;

		public string OrderType
		{
			get
			{
				return _OrderType;
			}
			set
			{
				_OrderType = value;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (SiteContext.Current == null)
			{
				Server.Transfer("~/Login.aspx");
			}

			if (!IsPostBack)
			{
				BindMyOrder();
			}
		}

		protected void ListViewOrders_ItemDataBound(object sender, ListViewItemEventArgs e)
		{
			if (e.Item.ItemType == ListViewItemType.DataItem)
			{
				if (SiteContext.Current.WorkingLanguage == null)
				{
					return;
				}

				var ListViewProducts = e.Item.FindControl("ListViewProducts") as ListView;
				var HiddenOrderID = e.Item.FindControl("HiddenOrderID") as HiddenField;

				if (_OrderService == null)
				{
					_OrderService = new Service.OrderService();
				}

				int theOrderID = 0;

				if (int.TryParse(HiddenOrderID.Value, out theOrderID))
				{
					if(_ProductService == null)
					{
						_ProductService = new Service.ProductService();
					}

					var ordersProducts = (from opm in _OrderService.GetOrderProductMappingByOrder(theOrderID)
										  join p in _ProductService.GetAllProductsByLanguage(SiteContext.Current.WorkingLanguage.LanguageId) on opm.ProductID equals p.ProductID
										  select new
										  {
											  OrderID = opm.OrderID,
											  ProductID = opm.ProductID,
											  Quantity = opm.Quantity,
											  ListPrice = p.ListPrice,
											  ProductName = p.Name,
											  ProductCode = p.ProductCode
										  });

					ListViewProducts.DataSource = ordersProducts;
					ListViewProducts.DataBind();
				}
			}
		}

		protected void ListViewOrders_PreRender(object sender, EventArgs e)
		{
			if (ListViewOrders.Items.Count > 0)
			{
				var LiteralCol1 = ListViewOrders.FindControl("LiteralCol1") as Literal;
				var LiteralCol2 = ListViewOrders.FindControl("LiteralCol2") as Literal;
				var LiteralCol3 = ListViewOrders.FindControl("LiteralCol3") as Literal;
				var LiteralCol4 = ListViewOrders.FindControl("LiteralCol4") as Literal;
				var LiteralCol5 = ListViewOrders.FindControl("LiteralCol5") as Literal;

				LiteralCol1.Text = GetLocalResourceString("MyAccount.OrderList.ProductInfo.ColumnTitle");
				LiteralCol2.Text = GetLocalResourceString("MyAccount.OrderList.PaymentMethod.ColumnTitle");
				LiteralCol3.Text = GetLocalResourceString("MyAccount.OrderList.OrderStatus.ColumnTitle");
				LiteralCol4.Text = GetLocalResourceString("MyAccount.OrderList.TotalPrice.ColumnTitle");
				LiteralCol5.Text = GetLocalResourceString("MyAccount.OrderList.ShippingAddress.ColumnTitle");
			}
		}

		private void BindMyOrder()
		{
			var currentUser = SiteContext.Current.User;

			if (currentUser == null)
			{
				Server.Transfer("~/Login.aspx");
			}

			var userID = currentUser.ProviderUserKey as Guid?;

			if (_OrderService == null)
			{
				_OrderService = new Service.OrderService();
			}

			if (OrderType.ToLower().Equals("processing"))
			{
				var orders = _OrderService.GetOrderByUser(userID.Value);

				if (orders != null)
				{
					var myOrders = (from o in orders
									join os in _OrderService.GetAllOrderStatus() on o.OrderStatusID equals os.OrderStatusID
									where os.OrderStatusNumber.Value != (int)Data.Entity.OrderStatuses.TRADE_CLOSED && os.OrderStatusNumber.Value != (int)Data.Entity.OrderStatuses.REFUND_CLOSED
									orderby o.CreatedOn descending
									select new
									{
										CreatedOn = o.CreatedOn,
										DeliveryDate = o.DeliveryDate,
										OrderNumber = o.OrderNumber,
										OrderID = o.OrderID,
										OrderStatusID = o.OrderStatusID.GetValueOrDefault(),
										OrderTotal = o.OrderTotal,
										PaidDate = o.PaidDate,
										PaymentMethodID = o.PaymentMethodID,
										PaymentMethodName = o.PaymentMethodName,
										PaymentStatusID = o.PaymentStatusID,
										ShippedDate = o.ShippedDate,
										ShippingAddress1 = o.ShippingAddress1,
										ShippingAddress2 = o.ShippingAddress2,
										ShippingCity = o.ShippingCity,
										ShippingCompany = o.ShippingCompany,
										ShippingCountry = o.ShippingCountry,
										ShippingCountryID = o.ShippingCountryID,
										ShippingFirstName = o.ShippingFirstName,
										ShippingLastName = o.ShippingLastName,
										ShippingMethodID = o.ShippingMethodID,
										ShippingMethod = o.ShippingMethod,
										ShippingStateProvince = o.ShippingStateProvince,
										ShippingStateProvinceID = o.ShippingStateProvinceID,
										ShippingStatusID = o.ShippingStatusID,
										TrackingNumber = o.TrackingNumber,
										UserID = o.UserID,
										UserLanguageID = o.UserLanguageID,
										ShippingCityID = o.ShippingCityID,
										InvoisingContent = o.InvoicingContent,
										InvoicingHeader = o.InvoicingHeader,
										Remark = o.Remark,
										OrderStatusName = os.Name
									});

					ListViewOrders.DataSource = myOrders;
					ListViewOrders.DataBind();
				}
			}
			else
			{
				var orders = _OrderService.GetOrderByUser(userID.GetValueOrDefault());

				if (orders != null)
				{
					var myOrders = (from o in orders
									join os in _OrderService.GetAllOrderStatus() on o.OrderStatusID equals os.OrderStatusID
									where os.OrderStatusNumber.Value == (int)Data.Entity.OrderStatuses.TRADE_CLOSED && os.OrderStatusNumber.Value == (int)Data.Entity.OrderStatuses.REFUND_CLOSED
									orderby o.CreatedOn descending
									select new
									{
										CreatedOn = o.CreatedOn,
										DeliveryDate = o.DeliveryDate,
										OrderNumber = o.OrderNumber,
										OrderID = o.OrderID,
										OrderStatusID = o.OrderStatusID.GetValueOrDefault(),
										OrderTotal = o.OrderTotal,
										PaidDate = o.PaidDate,
										PaymentMethodID = o.PaymentMethodID,
										PaymentMethodName = o.PaymentMethodName,
										PaymentStatusID = o.PaymentStatusID,
										ShippedDate = o.ShippedDate,
										ShippingAddress1 = o.ShippingAddress1,
										ShippingAddress2 = o.ShippingAddress2,
										ShippingCity = o.ShippingCity,
										ShippingCompany = o.ShippingCompany,
										ShippingCountry = o.ShippingCountry,
										ShippingCountryID = o.ShippingCountryID,
										ShippingFirstName = o.ShippingFirstName,
										ShippingLastName = o.ShippingLastName,
										ShippingMethodID = o.ShippingMethodID,
										ShippingMethod = o.ShippingMethod,
										ShippingStateProvince = o.ShippingStateProvince,
										ShippingStateProvinceID = o.ShippingStateProvinceID,
										ShippingStatusID = o.ShippingStatusID,
										TrackingNumber = o.TrackingNumber,
										UserID = o.UserID,
										UserLanguageID = o.UserLanguageID,
										ShippingCityID = o.ShippingCityID,
										InvoisingContent = o.InvoicingContent,
										InvoicingHeader = o.InvoicingHeader,
										Remark = o.Remark,
										OrderStatusName = os.Name
									});

					ListViewOrders.DataSource = myOrders;
					ListViewOrders.DataBind();
				}
			}
		}
	}
}